package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.jd.es.EsHotel;
import com.gxa.jd.es.respository.HotelRepository;
import com.gxa.jd.mapper.HotelMapper;
import com.gxa.jd.pojo.Hotel;
import com.gxa.jd.service.HotelService;
import com.gxa.entity.PageResult;
import org.elasticsearch.common.geo.GeoPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private HotelRepository hotelRepository;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<Hotel> findAll() {
        return hotelMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<Hotel> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Hotel> hotels = (Page<Hotel>) hotelMapper.selectAll();
        return new PageResult<Hotel>(hotels.getTotal(), hotels.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<Hotel> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return hotelMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Hotel> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Hotel> hotels = (Page<Hotel>) hotelMapper.selectByExample(example);
        return new PageResult<Hotel>(hotels.getTotal(), hotels.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @Override
    public Hotel findById(Integer id) {
        return hotelMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param hotel
     */
    @Override
    public void add(Hotel hotel) {
        hotelMapper.insert(hotel);
        saveEsHotel(hotel);
    }

    /**
     * 修改
     *
     * @param hotel
     */
    @Override
    public void update(Hotel hotel) {
        hotelMapper.updateByPrimaryKeySelective(hotel);
        saveEsHotel(hotel);
    }

    private void saveEsHotel(Hotel hotel) {
        EsHotel esHotel = new EsHotel();
        esHotel.setId(hotel.getId());
        esHotel.setName(hotel.getName());
        esHotel.setAddress(hotel.getAddress());
        esHotel.setCreationTime(new Date());
        esHotel.setRevisionTime(new Date());
        esHotel.setImages(hotel.getImages());
        esHotel.setIntroduction(hotel.getIntroduction());
        esHotel.setStatus(hotel.getStatus());
        // 纬度,经度
        GeoPoint geoPoint = new GeoPoint(hotel.getLongitude(), hotel.getDimension());
        esHotel.setGeo(geoPoint);
        hotelRepository.save(esHotel);
    }


    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        hotelMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Hotel.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 酒店名字
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 酒店地址
            if (searchMap.get("address") != null && !"".equals(searchMap.get("address"))) {
                criteria.andLike("address", "%" + searchMap.get("address") + "%");
            }
            // 图片列表
            if (searchMap.get("images") != null && !"".equals(searchMap.get("images"))) {
                criteria.andLike("images", "%" + searchMap.get("images") + "%");
            }
            // 酒店介绍
            if (searchMap.get("introduction") != null && !"".equals(searchMap.get("introduction"))) {
                criteria.andLike("introduction", "%" + searchMap.get("introduction") + "%");
            }

            // id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 审核状态
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }

        }
        return example;
    }

}
